package com.kexun.controller;

import com.github.pagehelper.PageInfo;
import com.kexun.common.PageResult;
import com.kexun.common.utils.ResponseEntity;
import com.kexun.entity.DO.Department;
import com.kexun.entity.DO.Shop;
import com.kexun.service.DepartmentService;
import com.kexun.service.ShopService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("system/department")
public class DepartmentController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private DepartmentService departmentService;

    @GetMapping("list")
    @RequiresPermissions(value = "system/department/list")
    public String list() {
        return "department/department_list";
    }

    @ResponseBody
    @PostMapping("list")
    @RequiresPermissions(value = "system/department/list")
    public PageResult list(String dpName, int page, int limit) {
        PageInfo<Department> deptList = departmentService.findDeptList(dpName, page, limit);
        List<Department> list = deptList.getList();
        long total = deptList.getTotal();
        return new PageResult(list, total);
    }


    @GetMapping("add")
    public String add(Model model) {
        List<Shop> shopList = shopService.findAllShopList();
        List<Department> departmentList = departmentService.findAllDepList();
        Department department1 = new Department();
        department1.setDpId(0);
        department1.setDpName("一级部门");
        departmentList.add(department1);
        model.addAttribute(shopList);
        model.addAttribute(departmentList);
        return "department/department_add";
    }

    @ResponseBody
    @PostMapping("add")
    public ResponseEntity add(@RequestBody Department department) {
        department.setDpCreateTime(new Date());
        departmentService.addDepartment(department);
        return new ResponseEntity().success();
    }

    @ResponseBody
    @PostMapping("del")
    public ResponseEntity del(int id) {
        departmentService.deleteById(id);
        return new ResponseEntity().success();
    }

    @GetMapping("edit")
    public String edit(int id, Model model) {
        List<Department> departmentList = departmentService.findAllDepList();
        Department department1 = new Department();
        department1.setDpId(0);
        department1.setDpName("一级部门");
        departmentList.add(department1);
        Department department = departmentService.findDepartmentById(id);
        List<Shop> shopList = shopService.findHasShopList(id);
        model.addAttribute(shopList);
        model.addAttribute(departmentList);
        model.addAttribute(department);
        return "department/department_edit";
    }

    @ResponseBody
    @PostMapping("edit")
    public ResponseEntity edit(@RequestBody Department department) {
        departmentService.editDepartment(department);
        return new ResponseEntity().success();
    }


}
